﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;

namespace GisSharpBlog.NetTopologySuite.Commons.Text
{
    public class ASCIIEncoding : System.Text.Encoding
    {
        public override int GetByteCount(char[] chars, int index, int count)
        {
            if (chars == null)
            {
                throw new ArgumentNullException();
            }
            else if (index < 0 || index > chars.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            else if (count < 0 || count > (chars.Length - index))
            {
                throw new ArgumentOutOfRangeException();
            }

            return count;
        }

        public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
        {
            if (chars == null)
            {
                throw new ArgumentNullException();
            }
            else if (bytes == null)
            {
                throw new ArgumentNullException();
            }
            else if (charIndex < 0 || charIndex > chars.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            else if (charCount < 0 || charCount > (chars.Length - charIndex))
            {
                throw new ArgumentOutOfRangeException();
            }
            else if (byteIndex < 0 || byteIndex > bytes.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            else if ((bytes.Length - byteIndex) < charCount)
            {
                throw new ArgumentException();
            }

            int count = charCount;
            char ch;
            while (count > 0)
            {
                count -= 1;
                ch = chars[charIndex++];
                if (ch < (char)0x80)
                {
                    bytes[byteIndex++] = (byte)ch;
                }
                else
                {
                    bytes[byteIndex++] = (byte)'?';
                }
            }
            return charCount;
        }

        public override int GetCharCount(byte[] bytes, int index, int count)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException();
            }
            else if (index < 0 || index > bytes.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            else if (count < 0 || count > (bytes.Length - index))
            {
                throw new ArgumentOutOfRangeException();
            }
            return count;
        }

        public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException();
            }
            else if (chars == null)
            {
                throw new ArgumentNullException();
            }
            else if (byteIndex < 0 || byteIndex > bytes.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            else if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
            {
                throw new ArgumentOutOfRangeException();
            }
            else if (charIndex < 0 || charIndex > chars.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            else if ((chars.Length - charIndex) < byteCount)
            {
                throw new ArgumentException();
            }

            int count = byteCount;
            while (count-- > 0)
            {
                char c = (char)(bytes[byteIndex++]);
                if (c > 127)
                {
                    c = '?';
                }
                chars[charIndex++] = c;
            }

            return byteCount;
        }

        public override int GetMaxByteCount(int charCount)
        {
            if (charCount < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            return charCount;
        }

        public override int GetMaxCharCount(int byteCount)
        {
            if (byteCount < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            return byteCount;
        }
    }
}
